package com.codeoss.framework.view.renderer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.codeoss.framework.context.YunContext;
import com.codeoss.framework.converter.IConverterStore;
import com.codeoss.framework.view.component.Component;
import com.codeoss.module.res.model.ResUser;
import com.codeoss.module.res.service.IResUserService;
import com.codeoss.module.workflow.model.WorkflowActivityInstance;
import com.codeoss.module.workflow.model.WorkflowComment;
import com.codeoss.module.workflow.model.WorkflowProcess;
import com.codeoss.module.workflow.service.IWorkflowActivityInstanceService;
import com.codeoss.module.workflow.service.IWorkflowCommentService;
import com.codeoss.module.workflow.service.IWorkflowProcessService;

import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

@Service("workflowstepsRenderer")
public class WorkflowStepsRenderer extends CommonComponentRenderer {
	@Autowired
	private IWorkflowProcessService workflowProcessService;
	@Autowired
	private IWorkflowActivityInstanceService workflowActivityInstanceService;
	@Autowired
	private IWorkflowCommentService workflowCommentService;
	@Autowired
	private IConverterStore converterStore;
	@Autowired
	private IResUserService resUserService;
	
	@Override
	protected PropSerializeMap startRenderProp(YunContext yunContext, Component component, ViewPropMap viewPropMap) {
		PropSerializeMap propSerializeMap = super.startRenderProp(yunContext, component, viewPropMap);
		
		WorkflowProcess workflowProcess=getWorkflowProcess(yunContext);
		if(workflowProcess!=null) {
			List<Map<String, Object>> wkfActivityList=getWorkflowActivityInstanceList(workflowProcess);
			propSerializeMap.put("wkfActivityList", wkfActivityList);
			for(int i=0;i<wkfActivityList.size();i++) {
				Map<String, Object> dataMap=wkfActivityList.get(i);
				Timestamp startDate=(Timestamp)dataMap.get("start_date");
				String sStartDate=converterStore.getConverter("datetime").convertObjectToString(startDate);
				dataMap.put("start_date", sStartDate);
				
				Timestamp endDate=(Timestamp)dataMap.get("end_date");
				if(endDate!=null) {
					String sEndDate=converterStore.getConverter("datetime").convertObjectToString(startDate);
					dataMap.put("end_date", sEndDate);
				}
				
				Long createBy=(Long)dataMap.get("create_by");
				ResUser resUser=resUserService.getById(createBy);
				dataMap.put("create_by", resUser.getName());
				
				Long updateBy=(Long)dataMap.get("update_by");
				ResUser updateUser=resUserService.getById(updateBy);
				dataMap.put("update_by", updateUser.getName());
			}
			
			List<Map<String, Object>> wkfCommentList=getWorkflowCommentList(workflowProcess);
			propSerializeMap.put("wkfCommentList", wkfCommentList);
			for(int i=0;i<wkfCommentList.size();i++) {
				Map<String, Object> dataMap=wkfCommentList.get(i);
				Timestamp createDate=(Timestamp)dataMap.get("create_date");
				String sCreateDate=converterStore.getConverter("datetime").convertObjectToString(createDate);
				dataMap.put("create_date", sCreateDate);
				
				Long createBy=(Long)dataMap.get("create_by");
				ResUser resUser=resUserService.getById(createBy);
				dataMap.put("create_by", resUser.getName());
			}
		}
		
		return propSerializeMap;
	}
	
	private WorkflowProcess getWorkflowProcess(YunContext yunContext) {
		 Object idValue = yunContext.getCurrentId();
		 if(idValue==null) {
			 return null;
		 }
		 QueryWrapper<WorkflowProcess> processQuery=new QueryWrapper<WorkflowProcess>();
		 processQuery.eq("biz_key", idValue+"");
		 processQuery.inSql("workflow_workflow_id","(select id from workflow_workflow where form_code='"+yunContext.getPage().getCode()+"' or mobile_form_code='"+yunContext.getPage().getCode()+"')");
		 WorkflowProcess workflowProcess=workflowProcessService.getOne(processQuery);
		 return workflowProcess;
	}
	
	private List<Map<String, Object>> getWorkflowActivityInstanceList(WorkflowProcess workflowProcess){
		 QueryWrapper<WorkflowActivityInstance> query=new  QueryWrapper<WorkflowActivityInstance>();
		 query.eq("workflow_process_id", workflowProcess.getId());
		 query.orderByAsc("id");
		 return workflowActivityInstanceService.listMaps(query);
	}
	
	private List<Map<String, Object>> getWorkflowCommentList(WorkflowProcess workflowProcess){
		 QueryWrapper<WorkflowComment> query=new  QueryWrapper<WorkflowComment>();
		 query.eq("workflow_process_id", workflowProcess.getId());
		 query.orderByAsc("id");
		 return workflowCommentService.listMaps(query);
	}
}
